Ext.Loader.setConfig({enabled:true});

Ext.application({
	name: 'App',
	launch: function() {
		
		Ext.create('Ext.Container', {
   			layout: 'fit',
   			items: [{
   				xtype: 'form',
   				title: 'Fast Food Management System - Login',
   				frame: true,
   				bodyPadding: 13,
   				height: null,

   				defaultType: 'textfield',
   				defaults: {
   					anchor: '100%',
   					listeners: {
   						specialkey: submitOnEnter
   					}
   				},

   				items: [{
   					allowBlank: false,
   					fieldLabel: 'Username',
   					id: 'username',
   					name: 'username',
   					emptyText: 'Username'
   				}, {
   					allowBlank: false,
   					fieldLabel: 'Password',
   					name: 'password',
   					id: 'password',
   					emptyText: 'Password',
   					inputType: 'password'
   				}, {
   					xtype: 'checkbox',
   					fieldLabel: 'Remember me',
   					name: 'remember'
   				}],

   				buttons: [{
   					text: 'Register'
   				}, {
   					text: 'Login',
   					handler: function() {
   						doSubmit(this.up('form').getForm());
   					}
   				}],
   				url: "login.action"
   			}],
   			renderTo : 'loginDiv',
   			listeners: {
   				afterrender : function() {
   					Ext.getCmp('username').focus(true,10);
   				}
   			}
   		});

   		function submitOnEnter(field, event) {
   			if(event.getKey() == event.ENTER) {
   				doSubmit(field.up('form').getForm());
   			}
   		}

   		var doSubmit = function(form) {
   			if(form.isValid()) {
   				// Submit the Ajax request and handle the response
   				form.submit({
   					waitTitle : 'Connecting',
   					waitMsg : 'Sending data...',
   					success : function(form, action) {
                        window.location.href = "admin";
   					},
   					failure : function(form, action) {
   						Utils.showMessageBox("Error", "Error");
   					}
   				});
   			}
   		};
		
	}
});